const express=require("express")
const router=express.Router();
const pool=require("../pool");
//汽车试驾
router.post("/drive",(req,res)=>{
  //1:获取参数
  var did=parseInt(req.body.did);    //商品id
  var dname = req.body.dname;        //试驾用户名
  var uid = req.session.uid;
  var dtimes = req.body.dtimes;
  var sellerid = parseInt(req.body.sellerid);
  var phone = req.body.phone;
  var sql=`SELECT id FROM yhc_drive WHERE did= ? AND dname= ? AND phone = ?`;
  console.log(uid);
  //先判断该用户是否预定试驾了该类型的车
  pool.query(sql,[did,dname,phone],(err,result)=>{
    if(err) throw err;
    if(result.length==0){//如果收藏数据库没有数据插
      var sql=` INSERT INTO yhc_drive
                VALUES(null,${did},${uid},
                '${dname}','${dtimes}',${sellerid},${phone})`;
      pool.query(sql,(err,result)=>{  
        if(err) throw err;
        if(result.affectedRows>0){
          res.send({code:1,msg:'预定成功'});
        }else{
          res.send({code:-1,msg:'添加失败'});
        }
      })
    }else{
      res.send({code:2,msg:'您已经预定试驾改类型的车了'})
    }
  })
})

//汽车预购
router.get("/buyorder",(req,res)=>{
  var did = parseInt(req.query.did);
  var id=req.session.uid;
  var sellerid = parseInt(req.query.sellerid);
  console.log(did,id,sellerid);
  var sql=`SELECT id FROM yhc_buy WHERE did= ? AND uid = ?`;
  pool.query(sql,[did,id],(err,result)=>{
    if(err) throw err;
    if(result.length==0){
      var sql=`INSERT INTO yhc_buy VALUES(null,${did},${id},${sellerid})`;
      pool.query(sql,(err,result)=>{  
        if(err) throw err;
        if(result.affectedRows>0){
          res.send({code:1,msg:'预定成功'});
        }else{
          res.send({code:-1,msg:'预定失败'});
        }
      })
    }else{
      res.send({code:2,msg:'您已经预定购买了此型号的车!'})
    }
  })
})

router.get("/drivelist",(req,res)=>{
  var uid=req.session.uid || 1;
  var driveData = {
    driveOrder:{},
    carInfo:{},
    sellerInfo:{}
  }

  //d：car_type i:car_details_info t:car_details_type p：
  // var sql=`SELECT d.cid,d.src,d.title,i.aprice,t.typename,p.uid,p.dtime,p.sellerid
  //          FROM car_type d,
  //          car_details_info i,
  //          car_details_type t,
  //          WHERE d.cid = c.did AND uid = ?`;
  new Promise((resolve,reject)=>{
   //先查询预约表，获得预约信息
    var sql =` SELECT id,did,dtime,sellerid
    FROM yhc_drive
    WHERE uid = ?`;
    pool.query(sql,[uid],(err,result)=>{
      if(err) throw err;
      driveData.driveOrder = result;
      console.log(result);
      resolve(true)
    })
  }).then(value=>{
    //查询信息
    return new Promise((resolve,result)=>{
      var did = driveData.driveOrder.did
      var sql =` SELECT d.cid,d.src,d.title,i.aprice,t.typename
                 FROM car_type d,
                 car_details_info i,
                 car_details_type t,
                 WHERE d.did = ? AND i.did = ? AND t.did =?
                `
      pool.query(sql,[did,did,did],(err,result)=>{
        if(err) throw err;
        driveData.carInfo = result;
        resolve(true)
      })
    })
  }).then(value=>{
    //查询商家信息
    return new Promise((resolve,result)=>{
      var sellerid = driveOrder.driveData.sellerid
      var sql =` SELECT id,title,adress
                 FROM yhc_4s
                 WHERE 
                `
      pool.query(sql,[sellerid],(err,result)=>{
        if(err) throw err;
        driveData.sellerInfo = result;
        res.send({code:1,data:driveData})
        res.end();
      })
    })
  })
  
          
 
})

module.exports=router;